package com.bnyy.medicalHousekeeper.moudle.message;

import android.content.Context;
import android.graphics.Color;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.bnyy.medicalHousekeeper.GlideHelper;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2022/11/10
 **/
public class MessageHeaderHelper {

    public static void setHeader(Context context, RelativeLayout relativeLayout, int imgResId) {

        init(context, relativeLayout, 1);

        GlideHelper.setCircleImage(context, imgResId, relativeLayout.findViewById(0));
    }


    public static void setHeader(Context context, RelativeLayout relativeLayout, String... imgs) {
        setHeader(context, relativeLayout, new ArrayList<>(Arrays.asList(imgs)));
    }

    public static void setHeader(Context context, RelativeLayout relativeLayout, ArrayList<String> imgs) {
//        relativeLayout.removeAllViews();

        if (imgs == null || imgs.size() == 0) {
            init(context, relativeLayout, 1);
            Glide.with(context).load(com.bnyy.message.R.mipmap.icon_default_header_man).into((ImageView) relativeLayout.findViewById(0));
        } else {
            int size = imgs.size();
            if (size > 4) {
                size = 4;
            }

            init(context, relativeLayout, size);

            for (int i = 0; i < size; i++) {
                GlideHelper.setCircleImage(context, imgs.get(i), relativeLayout.findViewById(i), size == 1 ? 0 : 1, Color.WHITE, com.bnyy.message.R.mipmap.icon_default_header_man);
            }
        }

    }

    private static void init(Context context, RelativeLayout relativeLayout, int size) {
        int headersSize = ScreenUtils.getScreenWidth() / 5;
        int headerSize = headersSize / 5 * 3;

        int dp5 = SizeUtils.dp2px(5);

        relativeLayout.removeAllViews();

        RelativeLayout rlContent = new RelativeLayout(context);

        relativeLayout.addView(rlContent);

        rlContent.setPadding(dp5, dp5, dp5, dp5);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(headersSize, headersSize);
        layoutParams.setMargins(dp5, dp5, dp5, dp5);
        rlContent.setLayoutParams(layoutParams);

        for (int i = 0; i < size; i++) {
            RelativeLayout.LayoutParams params;
            if (size == 1) {
                params = new RelativeLayout.LayoutParams(headersSize, headersSize);
                params.addRule(RelativeLayout.CENTER_IN_PARENT);
            } else {
                params = new RelativeLayout.LayoutParams(headerSize, headerSize);
                if (i == 0) {
                    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                } else {
                    switch (size) {
                        case 2:
                            params.addRule(RelativeLayout.ALIGN_PARENT_END);
                            break;
                        case 3:
                            switch (i) {
                                case 1:
                                    params.addRule(RelativeLayout.ALIGN_PARENT_END);
                                    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                                    break;
                                case 2:
                                    params.addRule(RelativeLayout.CENTER_HORIZONTAL);
                                    break;
                            }
                            break;
                        case 4:
                            switch (i) {
                                case 1:
                                    params.addRule(RelativeLayout.ALIGN_PARENT_END);
                                    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                                    break;
                                case 2:
                                    params.addRule(RelativeLayout.ALIGN_PARENT_END);
                                    break;
                            }
                            break;
                    }
                }
            }
            ImageView iv = new ImageView(context);
            iv.setId(i);
            rlContent.addView(iv, params);
        }
    }
}
